package fr.isen.smssender.contact;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.*;
import fr.isen.smssender.R;
import fr.isen.smssender.database.DBManager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class ContactListActivity extends Activity{

    private DBManager myDBManager;
    public static int position;
    String name = "null";
    String number= "00000";
    String contactId = "null";
    int numberContact =0;
    int i;
    static int boucle = 0;


    public static List<String> list = new ArrayList<String>();
    private static final int CONTACT_PICKER = 1;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_list);

        // Get back the phone's contacts
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.contactList);
        ListView listView = new ListView(this);
        linearLayout.addView(listView);
        listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, list));




        if ( boucle == 0){
            // cursor qui va récupérer les informations contenues dans le répertoire
            Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            startManagingCursor(cursor);


            while (cursor.moveToNext()) {
                numberContact = numberContact+1;
            }

            cursor.moveToFirst();

            String [] tab_name = new String[numberContact];
            String [] tab_number = new String[numberContact];
            String [] tab_ID = new String[numberContact];

            i = 0;

            while(i < numberContact){

                contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                tab_name[i]= name;
                tab_ID[i] = contactId;

                // Query the table
                Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
                while (phoneCursor.moveToNext()) {
                    number = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }

                tab_number[i]=number;
                i= i+1;
                String firstName = "";
                for(int index = 1; index < name.split(" ").length; index++){
                    firstName += name.split(" ")[index];
                }
                list.add("Nom: " + name.split(" ")[0] +" \nPrénom: "+firstName+" \nNuméro: " + tab_number[0]);
                refresh();


                phoneCursor.close();
                cursor.moveToNext();
            }

            boucle = boucle + 1 ;
            cursor.close();
        }




        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                setPosition(position);
                Bundle bundle = new Bundle();
                String stringToParse = (String)adapterView.getItemAtPosition(position);
                String[] parsedString = stringToParse.split(" ");
                String name = parsedString[1];
                String firstName = parsedString[3];
                String number = parsedString[5];
                bundle.putInt("id",-1);
                bundle.putString("name", name);
                bundle.putString("firstName", firstName);
                bundle.putString("number",number);
                Calendar c = Calendar.getInstance();
                bundle.putString("date",(new Date(c.get(Calendar.YEAR),c.get(Calendar.MONTH)+1,c.get(Calendar.DAY_OF_MONTH))).toString());
                newActivity(bundle);
            }
        });

        // Attention à revoir!
        final Button buttonAddContact = (Button) findViewById(R.id.buttonAddContact);
        buttonAddContact.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt("id",-1);
                newActivity(bundle);
            }
        });
    }



    public void refresh()
    {
        Intent i = new Intent(this, ContactListActivity.class);
        startActivity(i);
    }

    public void newActivity(Bundle bundle){

        Intent j = new Intent(this, ContactParameterActivity.class);
        j.putExtras(bundle);
        startActivity(j);
    }

    public static void setPosition(int nb)
    {
        position=nb;
    }

    public static int getPosition()
    {
        return position;
    }


}